
import time
from lxml import etree

import requests
#获取验证码
from ddddocr import DdddOcr
ocr = DdddOcr(show_ad=False)

t = int(time.time() *1000)

url = f"https://www.gushiwen.cn/RandCode.ashx?t={t}"
res_img = requests.get(url)
with open("./static/yzm/古诗文.jpg","wb") as f :
    f.write(res_img.content)

r = ocr.classification(res_img.content)

#获取请求参数

url_login = "https://www.gushiwen.cn/user/login.aspx"
res_login = requests.post(url_login)
html = res_login.text

tree = etree.HTML(html)
__VIEWSTATE = tree.xpath("//input[@id='__VIEWSTATE']/@value")[0]
__VIEWSTATEGENERATOR = tree.xpath("//input[@id='__VIEWSTATEGENERATOR']/@value")[0]

datas = {
    "__VIEWSTATE": __VIEWSTATE,
    "__VIEWSTATEGENERATOR": __VIEWSTATEGENERATOR,
    "email":"18436276328",
    "pwd":"hbg123666",
    "denglu":"登录",
    "code":f"{r}"
}

#发起登录请求
url_denlgu = "https://www.gushiwen.cn/user/login.aspx"

res_denglu = requests.post(url_denlgu,data=datas,cookies=res_img.cookies)
# print(res_img.cookies)
# print(res_denglu.text)
print(res_img.cookies)
print(res_denglu.cookies)

res_collect = requests.get('https://www.gushiwen.cn/user/collect.aspx', cookies=res_denglu.cookies)

tree = etree.HTML(res_collect.text)

items = tree.xpath('//div[@class="sons"]/div[@class="cont"]/a')
for item in items:
    print(item.xpath('./@href'), item.xpath('./text()'))
# __VIEWSTATE
# /wEPDwUKLTU5OTg0MDIwNw8WAh4TVmFsaWRhdGVSZXF1ZXN0TW9kZQIBZGQGi0FCmPHMP+KelvQVsoBoqE2Axg==
# __VIEWSTATEGENERATOR
# C93BE1AE
# from
# email
# 18436276328
# pwd
# hbg123666
# code
# ifwc
# denglu
# 登录